package pwd.allen.intercept;

import net.bytebuddy.implementation.bind.annotation.Origin;
import net.bytebuddy.implementation.bind.annotation.RuntimeType;
import net.bytebuddy.implementation.bind.annotation.SuperCall;

import java.lang.reflect.Method;
import java.util.concurrent.Callable;

/**
 * @author 门那粒沙
 * @create 2021-03-09 22:23
 **/
public class MyInterceptor {

    /**
     * 打印方法的执行时间
     * 注意：@Origin注解别用错成net.bytebuddy.asm.Advice.Origin
     *
     * @param method
     * @param callable
     * @return
     * @throws Exception
     */
    @RuntimeType
    public static Object intercept(@Origin Method method, @SuperCall Callable<?> callable) throws Exception {
        Long start = System.currentTimeMillis();
        try {
            return callable.call();
        } finally {
            System.out.println(String.format("当前方法%s，用时%sms", method.toGenericString(), (System.currentTimeMillis() - start)));
        }
    }
}
